package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"medical/models"
	"strconv"
)

type MedicalController struct {
	beego.Controller
}

type JsonRes struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *MedicalController) Add() {
	name := c.GetString("name")
	sex := c.GetString("sex")
	tel := c.GetString("tel")
	zrDocker := c.GetString("zrDocker")
	uptime := c.GetString("uptime")
	number, _ := c.GetInt("number")
	cate := c.GetString("cate")
	state := c.GetString("state")
	if name == "" || sex == "" || tel == "" || zrDocker == "" || uptime == "" || number == 0 || cate == "" || state == "" {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "格式错误",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	medical := models.Medical{
		Name:     name,
		Sex:      sex,
		Tel:      tel,
		ZrDoctor: zrDocker,
		Uptime:   uptime,
		Number:   number,
		Cate:     cate,
		State:    state,
	}
	err := models.DB.Create(&medical).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *MedicalController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 10)
	query := models.DB.Model(&models.Medical{})
	name := c.GetString("name")
	if name != "" {
		query = query.Where("name like ?", "%"+name+"%")
	}
	key := "page-" + strconv.Itoa(page)
	val, err := models.Rdb.Get(key).Result()
	pageData := make(map[string]interface{})
	if err != nil {
		var count int64
		query.Count(&count)

		var medical []models.Medical
		offset := (page - 1) * size
		err = query.Limit(size).Offset(offset).Find(&medical).Error
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
		}
		pageData["count"] = count
		pageData["medical"] = medical
		jsonByte, _ := json.Marshal(&pageData)
		value := string(jsonByte)
		models.Rdb.Set(key, value, 0)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "查询成功-mysql",
			Data: pageData,
		}
		c.ServeJSON()
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.ServeJSON()
	}
}
func (c *MedicalController) Info() {
	id, _ := c.GetInt("id")
	key := "medical-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	RankKey := "medical-rank"
	var medical models.Medical
	if err != nil {
		err = models.DB.Where("id=?", id).Find(&medical).Error
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "详情查询失败",
				Data: nil,
			}
			c.ServeJSON()
		}
		models.Rdb.ZRevRangeWithScores(RankKey, 1, 0)
		jsonByte, _ := json.Marshal(&medical)
		value := string(jsonByte)
		models.Rdb.Set(key, value, 0)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "详情查询成功-mysql",
			Data: medical,
		}
		c.ServeJSON()
	} else {
		models.Rdb.ZRevRangeWithScores(RankKey, 1, 0)
		json.Unmarshal([]byte(val), &medical)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "查询详情成功-redis",
			Data: medical,
		}
		c.ServeJSON()
	}
}

//func (c *MedicalController) Rank() {
//	key := "medical-rank"
//}

func (c *MedicalController) Update() {
	id, _ := c.GetInt("id")
	cate := c.GetString("cate")
	err := models.DB.Where("id= ?", id).First(&cate).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "修改失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "修改成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
